using Avalonia.BiliBili.Views.ChildViews.User;
using Avalonia.Controls;
using Avalonia.Extensions.Controls;
using Avalonia.Input;
using Bilibili.Common;
using Bilibili.Model.Collection;
using Bilibili.Model.Common;
using Bilibili.WebClient.Api;
using Bilibili.WebClient.Common;
using DynamicData;
using PCLUntils.IEnumerables;
using PCLUntils.Objects;
using System;
using System.Collections.ObjectModel;

namespace Avalonia.BiliBili.Views.ChildViews
{
    public partial class CollectionView : ChildView
    {
        private int PageNum { get; set; } = 1;
        private CollectionClient Client { get; }
        private ObservableCollection<GetUserFovBoxList> Types { get; }
        private ObservableCollection<FavouriteBoxsVideoItem> List { get; }
        public CollectionView()
        {
            InitializeComponent();
            Client = new CollectionClient();
            Types = [];
            List = [];
        }
        public override void Callback(object data) { }
        public override void AfterInitializeComponent()
        {
            typeList.SelectionChanged += TypeList_SelectionChanged;
            typeList.ItemsSource = Types;
            collectionList.ItemClick += CollectionList_ItemClick;
            collectionList.ItemsSource = List;
            RunOnUiThread(async () =>
            {
                var result = await Client.GetFavType();
                if (result.IsSuccess())
                {
                    var model = result.Data;
                    if (model.IsNotEmpty() && model.Message.Equals("success", StringComparison.CurrentCultureIgnoreCase) && model.Data != null)
                    {
                        var data = model.Data.List;
                        if (data != null && data.Count > 0)
                        {
                            Types.AddRange(data);
                            typeList.SelectedIndex = 0;
                        }
                    }
                }
                else
                    PopupToast.Show(result.Message);
            });
        }
        private void CollectionList_ItemClick(object? sender, Extensions.Event.ViewRoutedEventArgs e)
        {
            try
            {
                if (e.ClickMouse == MouseButton.Left && e.ClickItem is GridViewItem item && item.Content is FavouriteBoxsVideoItem model)
                {
                    if (model.Aid.IsNotEmpty())
                        App.Mapper.NavigateToPlayer(model.Aid, PlayMode.Video);
                }
            }
            catch { }
        }
        private void TypeList_SelectionChanged(object? sender, SelectionChangedEventArgs e)
        {
            RunOnUiThread(async () =>
            {
                PageNum = 1;
                if (e.AddedItems.FindItem(0) is GetUserFovBoxList item)
                {
                    if (ApiProvider.Instance.IsLogin)
                    {
                        List.Clear();
                        var result = await Client.GetVideoFavBox(item.Fid, PageNum);
                        if (result.IsSuccess())
                        {
                            var model = result.Data;
                            if (model.IsNotEmpty() && model.Code == 0)
                            {
                                var data = model.Data.Archives;
                                if (data.IsNotEmpty())
                                    List.AddRange(data);
                            }
                        }
                    }
                }
            });
        }
    }
}